package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctServiceTaskBo;
import com.rzdata.poct.system.domain.business.po.PoctServiceTask;
import com.rzdata.poct.system.domain.business.vo.PoctServiceTaskVo;
import com.rzdata.poct.system.mapper.PoctServiceTaskMapper;
import com.rzdata.poct.system.service.IPoctServiceTaskService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctServiceTaskServiceImpl extends ServicePlusImpl<PoctServiceTaskMapper, PoctServiceTask, PoctServiceTaskVo> implements IPoctServiceTaskService {

    @Override
    public PoctServiceTaskVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctServiceTaskVo> queryPageList(PoctServiceTaskBo bo) {
        PagePlus<PoctServiceTask, PoctServiceTaskVo> result =
            pageVo(PageUtils.buildPagePlusNew(Long.valueOf(bo.getPageNum()), Long.valueOf(bo.getPageSize())), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctServiceTaskVo> queryList(PoctServiceTaskBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctServiceTask> buildQueryWrapper(PoctServiceTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctServiceTask> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTaskNo()), PoctServiceTask::getTaskNo, bo.getTaskNo());
        lqw.like(StringUtils.isNotBlank(bo.getInstrumentNo()), PoctServiceTask::getInstrumentNo, bo.getInstrumentNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMaintainer()), PoctServiceTask::getMaintainer, bo.getMaintainer());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), PoctServiceTask::getContact, bo.getContact());
        lqw.eq(bo.getNextService() != null, PoctServiceTask::getNextService, bo.getNextService());
        lqw.eq(StringUtils.isNotBlank(bo.getResult()), PoctServiceTask::getResult, bo.getResult());
        if (ObjectUtil.isNotEmpty(bo.getStartTime())) {
            lqw.ge(PoctServiceTask:: getCreateTime, bo.getStartTime());
        }
        if (ObjectUtil.isNotEmpty(bo.getEndTime())) {
            lqw.le(PoctServiceTask:: getCreateTime, bo.getEndTime());
        }
        if (ObjectUtil.isNotEmpty(bo.getCreateTime())) {
            lqw.ge(PoctServiceTask:: getCreateTime, bo.getCreateTime());
        }
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctServiceTaskBo bo) {
        PoctServiceTask add = BeanUtil.toBean(bo, PoctServiceTask.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctServiceTaskBo bo) {
        PoctServiceTask update = BeanUtil.toBean(bo, PoctServiceTask.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctServiceTask entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
